<?php
namespace app\admin\controller;

use app\admin\tool\Navigation;
use think\Controller;
use think\Request;

class Base extends Controller {
    use Navigation;
    protected $data;
    protected $admin;

    public function __construct(Request $request)
    {
        $navigation = $this->getNavigation();
        $this->data['navigation'] = $navigation;
        $this->data['module'] = strtolower($request->module());
        //controller 比较特殊 不能用strtolower 要把第一个字母改成小写
        $this->data['controller'] = lcfirst($request->controller());
        $this->data['action'] = strtolower($request->action());
        $this->data['permission'] = $this->data['module'].'/'.$this->data['controller'].'/'.$this->data['action'];

        $this->checkLogin();
        $this->checkPermission();
    }


    protected function checkLogin(){
        $this->admin = session('admin');
        if(!$this->admin){
            $this->error("你还没有登录",url('admin/login/index'));
        }
        $this->data['admin'] = $this->admin;

        //忽略列表，所有人都可以看的 目前就这一个
        array_push($this->admin->permission,'admin/index/index');
        //var_dump($this->admin->permission);exit;
    }

    protected function checkPermission(){
        if($this->admin->is_super){
            return;
        }

        if(empty($this->admin->permission)){
            $this->error('没有操作权限',url('admin/login/index'));
        }
        //echo $this->data['permission'];var_dump($this->admin->permission);exit;
        if(!in_array($this->data['permission'],$this->admin->permission)){

            $this->error('没有操作权限');
        }
    }


}